"""
预约设置服务类. 将一个服务, 封装成一个类.
服务名: OrderSettingService            类名: OrderSettingService
方法名1: add                           方法名1: add
方法名2: getOrderSettingByMonth        方法名2: get_order_setting_by_month
方法名3: editNumberByDate              方法名3: edit_number_by_date
"""
import json

from api.base_service import BaseService


class OrderSettingService(BaseService):

    def __init__(self):
        # 在子类中, 增加服务名
        super().__init__()
        self.service_name = "OrderSettingService"

    # 添加预约设置 void add(List<OrderSetting> list)
    def add(self, data_list):
        resp = self.client.invoke(self.service_name, "add", data_list)
        # 成功: [{"orderDate": "2022-08-04 15:48:30", "number": 900}] 返回 "null"
        # 失败: [{"orderDate": "2022-08-04", "number": 900}] 返回 "Failed" 缺少时分秒 --> 接口的问题, 不用纠结
        # 失败: {"orderDate": "2022-08-04 15:48:30", "number": 900} 返回 "No" 数据类型错误, 传入的字典
        # 这个接口不需要转换JSON数据, 成功就返回True, 失败就返回False
        if resp == "null":
            return True
        else:
            return False

    # 按月统计预约设置信息 List getOrderSettingByMonth(String date)
    def get_order_setting_by_month(self, date_str):
        resp = self.client.invoke(self.service_name, "getOrderSettingByMonth", date_str)
        # 成功 "2022-05"  返回 "[]"
        # 失败 2022 返回 "Failed"
        if resp == "Failed":
            return None
        else:
            return json.loads(resp)

    # 根据日期修改预约设置数量 void editNumberByDate(OrderSetting orderSetting)
    def edit_number_by_date(self, data_dict):
        data_dict["class"] = "com.itheima.pojo.OrderSetting"
        resp = self.client.invoke(self.service_name, "editNumberByDate", data_dict)
        # 成功: "null" {"orderDate": "2022-08-05 16:50:00", "number": 555, "class": "com.itheima.pojo.OrderSetting"}
        # 失败: "Failed" 缺少时分秒信息  {"orderDate": "2022-08-05", "number": 555, "class": "com.itheima.pojo.OrderSetting"}
        # 失败: "No" 缺少class字段信息  {"orderDate": "2022-08-05 16:50:00", "number": 555}
        # 这个接口不需要转换JSON数据, 成功就返回True, 失败就返回False
        if resp == "null":
            return True
        else:
            return False


if __name__ == '__main__':
    # 创建对象
    oss = OrderSettingService()

    # 添加预约设置
    data = [{"orderDate": "2022-08-04 15:48:30", "number": 9527}]
    res = oss.add(data)
    print("响应类型", type(res))
    print("响应数据", res)

    print("*" * 50)
    # 按月统计预约设置信息
    res = oss.get_order_setting_by_month("2020-08")
    print("响应类型", type(res))
    print("响应数据", res)

    print("*" * 50)
    # 根据日期修改预约设置数量
    data = {"orderDate": "2022-08-04 15:48:30", "number": 8888}
    res = oss.edit_number_by_date(data)
    print("响应类型", type(res))
    print("响应数据", res)
